package com.itheima.controller;

import com.itheima.pojo.Book;
import com.itheima.service.BookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    //quan bu
    @GetMapping
    public Result findAll() {
        List<Book> list = bookService.findAll();
        return new Result(Code.FANDALL_SEC, list, "查询成功");
    }
    //cha id
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Book book = bookService.findById(id);
        return new Result(Code.FANDBYID_SEC,book,"查询成功");
    }
    //zeng
    @PostMapping
    public Result save(@RequestBody Book book){
        boolean b = bookService.save(book);
        if(b){return new Result(Code.SAVE_SEC,null,"添加成功");}
        return new Result(Code.SAVE_ERR,null,"添加失败");
    }
    //shan
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean b = bookService.delete(id);
        if(b){return new Result(Code.DELETE_SEC,null,"删除成功");}
        return new Result(Code.DELETE_ERR,null,"删除失败");
    }
    //gai
    @PutMapping
    public Result update(@RequestBody Book book){
        boolean b = bookService.update(book);
        if(b){return new Result(Code.UPDATE_SEC,null,"修改成功");}
        return new Result(Code.UPDATE_ERR,null,"修改失败");
    }

}
